package com.platform.base.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.base.domain.*;
import com.platform.base.mapper.BaseFactoryMapper;
import com.platform.base.mapper.BaseLocationMapper;
import com.platform.common.constant.UserConstants;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.DateUtils;
import com.platform.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.platform.base.mapper.TempInfoMapper;
import com.platform.base.service.ITempInfoService;
import com.platform.common.core.text.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 温度采集器Service业务层处理
 * 
 * @author wangqi
 * @date 2020-09-26
 */
@Service
public class TempInfoServiceImpl extends ServiceImpl<TempInfoMapper, TempInfo> implements ITempInfoService
{
    @Autowired
    private TempInfoMapper tempInfoMapper;

    @Autowired
    private BaseFactoryMapper baseFactoryMapper;

    @Autowired
    private BaseLocationMapper baseLocationMapper;

    /**
     * 查询温度采集器列表
     * 
     * @param tempInfo 温度采集器
     * @return 温度采集器
     */
    @Override
    public List<TempInfo> selectTempInfoList(TempInfo tempInfo)
    {
        return tempInfoMapper.selectTempInfoList(tempInfo);
    }

    /**
     * 查询温度采集器
     *
     * @param tempId 温度采集器Id
     * @return 温度采集器
     */
    @Override
    public TempInfo selectTempInfoById(String tempId)
    {
        return tempInfoMapper.selectTempInfoById(tempId);
    }

    /**
     * 新增温度采集器
     * 
     * @param tempInfo 温度采集器
     * @return 结果
     */
    @Override
    public int insertTempInfo(TempInfo tempInfo)
    {
        tempInfo.setCreateTime(DateUtils.getNowDate());
        return tempInfoMapper.insert(tempInfo);
    }

    /**
     * 修改温度采集器
     * 
     * @param tempInfo 温度采集器
     * @return 结果
     */
    @Override
    public int updateTempInfo(TempInfo tempInfo)
    {
        tempInfo.setUpdateTime(DateUtils.getNowDate());
        return tempInfoMapper.updateById(tempInfo);
    }

    /**
     * 删除温度采集器对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTempInfoByIds(String ids)
    {
        return tempInfoMapper.deleteTempInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 校验温度采集器是否唯一
     *
     * @param tempInfo 温度采集器信息
     * @return 结果
     */
    @Override
    public String checkTempUnique(TempInfo tempInfo){
        TempInfo info = tempInfoMapper.selectById(tempInfo.getTempId());
        if (StringUtils.isNotNull(info))
        {
            return UserConstants.TEMP_ID_NOT_UNIQUE;
        }
        return UserConstants.TEMP_ID_UNIQUE;
    }

    /**
     * 导入温度采集器信息数据
     *
     * @param infoList 温度采集器信息数据
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importInfo(List<TempInfo> infoList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(infoList) || infoList.size() == 0)
        {
            throw new BusinessException("导入温度采集器信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TempInfo info : infoList)
        {
            try
            {
                //查询阀门类型信息
                BaseFactory factory = null;
                if(info.getFactoryName()!=null && !info.getFactoryName().equals("")){
                    List<BaseFactory> list = baseFactoryMapper.selectList(new QueryWrapper<BaseFactory>().eq("factory_name", info.getFactoryName()));
                    if(list!=null && list.size()>0){
                        factory = list.get(0);
                        info.setFactoryId(factory.getFactoryId());
                    }
                }
                //查询采集器地址信息
                BaseLocation location = null;
                if(info.getLocationName()!=null && !info.getLocationName().equals("")){
                    List<BaseLocation> list = baseLocationMapper.selectList(new QueryWrapper<BaseLocation>().eq("location_name", info.getLocationName()));
                    if(list!=null && list.size()>0){
                        location = list.get(0);
                        info.setLocationId(location.getLocationId());
                    }
                }
                // 校验采集器是否存在
                TempInfo f = tempInfoMapper.selectById(info.getTempId());
                if (StringUtils.isNull(f))
                {
                    info.setCreateBy(operName);
                    info.setCreateTime(DateUtils.getNowDate());
                    this.insertTempInfo(info);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、温度采集器 " + info.getTempId() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    info.setUpdateBy(operName);
                    info.setUpdateTime(DateUtils.getNowDate());
                    this.updateTempInfo(info);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、温度采集器 " + info.getTempId() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、温度采集器 " + info.getTempId() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、温度采集器 " + info.getTempId() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
